package com.mochat.threadPool;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

import com.mochat.api.logger.LoggerRemoteServiceI;
import com.mochat.listener.MyApplicationContextListener;
import com.mochat.model.RandomMessageBody;
import com.mochat.model.RoomMessageBody;
import com.mochat.redis.CustomRedisClusters;
import com.util.MoThreadFactory;
import com.util.MoThreadPoolExecutor;

public class ChatLoggerThreadPool {
	private static final Logger LOGGER = Logger.getLogger(NettyRandomThreadPool.class);
	private static final LinkedBlockingQueue<Runnable> LINKED_BLOCKING_QUEUE = new LinkedBlockingQueue<Runnable>();
	
	private static final MoThreadPoolExecutor MO_THREAD_POOL_EXECUTOR = new MoThreadPoolExecutor(3, 10, 600, TimeUnit.SECONDS, LINKED_BLOCKING_QUEUE, new MoThreadFactory("SEMD_MSG"));
	private ChatLoggerThreadPool() {}
	
	public static void send(Object msg) {
		MO_THREAD_POOL_EXECUTOR.execute(new SendRunable(msg));
	}
	
	private static class SendRunable implements Runnable{

		private Object message;
		
		public  SendRunable(Object msg) {
          this.message = msg;
		}
		
		@Override
		public void run() {
			LoggerRemoteServiceI loggerRemoteServiceI = (LoggerRemoteServiceI) MyApplicationContextListener.getBean("logRemoteServiceI");

			if(message instanceof RoomMessageBody) {
				loggerRemoteServiceI.insertRoomLogger((RoomMessageBody)message);
				return;
			}
			if(message instanceof RandomMessageBody) {
				loggerRemoteServiceI.insertRandomLogger((RandomMessageBody)message);
			}
		}
		
	}
}
